package com.pmsystem.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.pmsystem.pojo.News;
import com.pmsystem.pojo.Office;
import com.pmsystem.service.OfficeService;


/**
 * 
* <p>Title: OfficeController</p>  
* <p>Description:办公室信息的控制器 </p>  
* @author 树昌  
* @date 2019年1月24日
 */
@Controller
@RequestMapping("office/")
public class OfficeController {

	@Autowired
	OfficeService officeService;
	
	/**
	 * 
	 * <p>Title: addOffice</p>  
	 * <p>Description:添加一条办公室的记录 </p>  
	 * @author 树昌
	 * @param office
	 * @return 
	 */
	@RequestMapping(value="addoffice",method=RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> addOffice(Office office) {
		
		Map<String, Object> map = new HashMap<>();
		String msg = null;
		boolean row = officeService.addOffice(office);
		if(row) {
			msg = "success";
		}else {
			msg = "error";
		}
		map.put("msg", msg);
		return map;
	}
	
	
	/**
	 * 
	 * <p>Title: showOffice</p>  
	 * <p>Description:查询办公室信息的记录 </p>  
	 * @author 树昌
	 * @param keyword	搜索关键词
	 * @param startRow	页码
	 * @param pageSize	每页显示记录的条数
	 * @return	
	 */
	@RequestMapping(value="showoffice",method=RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> showOffice(String keyword, Integer startRow, Integer pageSize){
		
		Map<String, Object> map = new HashMap<>();
		List<Office> listoffice = officeService.showOffice(keyword, startRow, pageSize);
		map.put("listoffice", listoffice);
		int size = listoffice.size();
		map.put("size", size);
		return map;
	}
	
	/**
	 * 
	 * <p>Title: showOneOffice</p>  
	 * <p>Description:根据ID查询一条办公室记录 </p>  
	 * @author 树昌
	 * @param id
	 * @return
	 */
	@RequestMapping(value="showOneOffice",method=RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> showOneOffice(int id){
		
		Map<String, Object> map = new HashMap<>();
		Office office = officeService.showOneOffice(id);
		map.put("office", office);
		return map;
	}
	
	
	/**
	 * 
	 * <p>Title: deleteOffice</p>  
	 * <p>Description:删除一条办公室记录的控制器 </p>  
	 * @author 树昌
	 * @param office
	 * @return 不做跳转
	 */
	@RequestMapping(value="deleteoffice",method=RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> deleteOffice(int id){
		
		Map<String, Object> map = new HashMap<>();
		String msg = null;
		Office office = new Office();
		office.setOfficeAreaId(id);
		boolean row = officeService.deleteOffice(office);
		if(row) {
			msg = "success";
		}else {
			msg = "error";
		}
		map.put("msg", msg);
		return map;
	}
	
	
	/**
	 * 
	 * <p>Title: deleteAllOffice</p>  
	 * <p>Description:批量删除办公室区域信息 </p>  
	 * @author 树昌
	 * @param id
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value="deleteAllOffice",method=RequestMethod.POST)
	public Map<String, Object> deleteAllOffice(int [] id) {
		Map<String, Object> map = new HashMap<>();
		String msg=null;
		List<Integer> listID = new ArrayList<>();
		for (int i : id) {
			listID.add(i);
		}
		
		boolean row = officeService.deleteAllOffice(listID);
		
		if(row) {
			msg = "success";
		}else {
			msg = "error";
		}
		map.put("msg", msg);
		return map;
	}
	
	
	/**
	 * 
	 * <p>Title: updateOffice</p>  
	 * <p>Description:修改一条办公室信息 </p>  
	 * @author 树昌
	 * @param office
	 * @return
	 */
	@RequestMapping(value="updateoffice",method=RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> updateOffice(Office office) {
		
		Map<String, Object> map = new HashMap<>();
		String msg = null;
		boolean row = officeService.updateOffice(office);
		if(row) {
			msg = "success";
		}else {
			msg = "error";
		}
		map.put("msg", msg);
		return map;
	}
	
	
}